﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.LinqToSqlProvider;
using MvcEngine.Gallery.Repositories;
using MvcEngine.Gallery.Models;
using MvcEngine.Core.Pagination;
using MvcEngine.Core.Extensions;
using MvcEngine.Core.Base;

namespace MvcEngine.Gallery.LinqToSqlProvider
{
    public class GalleryImageRepository : Repository, IGalleryImageRepository
    {
        public GalleryImage Get(Guid id)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.GalleryImages().SingleOrDefault(p => p.Id == id);
            }
        }

        public void Add(GalleryImage image)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.GalleryImages().InsertOnSubmit(image);
                contextManager.Commit();
            }
        }

        public void Update(GalleryImage image)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Commit();
            }
        }

        public void Delete(Guid id)
        {
            var image = Get(id);
            if (image != null)
            {
                using (var contextManager = CreateContextManager(false))
                {
                    contextManager.Context.GalleryImages().DeleteOnSubmit(image);
                    contextManager.Commit();
                }
            }
        }

        public IPagination<GalleryImage> GetByCategory(Guid categoryId, LoadOptions options)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return (from p in contextManager.Context.GalleryImages()
                        join pc in contextManager.Context.GalleryImageCategories() on p.Id equals pc.ImageId
                        join c in contextManager.Context.GalleryCategories() on pc.CategoryId equals c.Id
                        where c.Id == categoryId
                        select p).Distinct().Sort(p => p.CreatedDate, options.SortAscending).GetPage(options.PageNumber, options.PageSize);
            }
        }

        public IPagination<GalleryImage> GetByGallery(Guid galleryId, LoadOptions options)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return (from p in contextManager.Context.GalleryImages()
                        join pc in contextManager.Context.GalleryImageCategories() on p.Id equals pc.ImageId
                        join c in contextManager.Context.GalleryCategories() on pc.CategoryId equals c.Id
                        join g in contextManager.Context.Galleries() on c.GalleryId equals g.Id
                        where g.Id == galleryId
                        select p).Distinct().Sort(p => p.CreatedDate, options.SortAscending).GetPage(options.PageNumber, options.PageSize);
            }
        }
    }
}
